var app = getApp();
Page({
    data: {
        addressData:[],         // 默认地址数据
        goodsList:[],           // 商品数据
        goods_id: '',           // 商品id
        total:0,                // 合计

        auction_id:"",          // 活动id
    },
    submit(){
        var self = this;
        var goods_id = this.data.goods_id;
        var address_id = this.data.addressData.id;
        var auction_id = this.data.auction_id
        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + '/order/save_order',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        ids:goods_id,
                        address_id,
                        auction_id
                    },
                    success: function (res) {
                        var data = res.data;
                        if (data.code == 1) {
                            wx.navigateTo({
                                url: '../pay-style/pay-style?money=' + data.data.money + '&order_code=' + data.data.trade_no,
                            })
                        }else{
                            wx.showToast({
                                image:'../../img/warning.png',
                                title: data.msg,
                            })
                        }
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({ goods_id: options.id })
        var total = 0;
        var self = this;
        wx.getStorage({
            key: 'user',
            success: function (res) {
                console.log(res.data)
                wx.request({
                    url: app.url.requestUrl + '/order/group_order_data',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        ids: options.id
                    },
                    success: function (res) {
                        if (res.data.code == 1) {
                            
                            res.data.data.map((item,index) => {
                                total += (item.price ? item.price : 0);
                            })
                            self.setData({
                                goodsList: res.data.data,
                                total: total
                            })
                        }
                    }
                })
            }
        })
    },
    // 获取默认地址数据
    getAddress() {
        var self = this;
        // /personal/default_address
        wx.getStorage({
            key: 'user',
            success: function (res) {
                console.log(res.data)
                wx.request({
                    url: app.url.requestUrl + '/personal/default_address',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                    },
                    header: {
                        'content-type': 'application/json'
                    },
                    success: function (res) {
                        if (res.data.code == 1) {
                            self.setData({
                                addressData: res.data.data
                            })
                        }
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getAddress();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})